/**
 * 
 */
package wdmsim;

import java.util.Comparator;
import java.util.PriorityQueue;
import events.*;

/**
 * @author alissonslp
 *
 */
public final class EventScheduler {

	private static EventScheduler eventScheduler = null;
	
	private PriorityQueue<Event> eventQueue;
	
	private static class EventSort implements Comparator<Event> {

        @Override
        public int compare(Event o1, Event o2) {
            if (o1.getTime() < o2.getTime()) {
                return -1;
            }
            if (o1.getTime() > o2.getTime()) {
                return 1;
            }
            return 0;
        }
    }
	
	public static EventScheduler getInstance() {
    	if (eventScheduler == null) 
    		eventScheduler = new EventScheduler();
    	
    	return(eventScheduler);
    }
	
	private EventScheduler() {
        EventSort eventSort = new EventSort();
        eventQueue = new PriorityQueue<Event>(10000, eventSort);
    }
	
	public Event popEvent() {
		
		Event event = eventQueue.poll();
    	
        return (event);
	}


	/**
	 * @param routeRequestEvent
	 * @param time
	 */
	public boolean schedule(Event event, long time) {
		event.setTime(time);
    	return (eventQueue.add(event));
	}
}
